package com.cz.nongyetongv5.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.cz.nongyetongv5.bean.RegisterBean;
import com.cz.nongyetongv5.bean.RegisterPic;
import com.cz.nongyetongv5.custom.dialog.CustomMyDialog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

public class RegisterDetailActivity extends AppCompatActivity implements View.OnClickListener {
    private RelativeLayout btnBack;
    RecyclerView recyclerView;


    private LinearLayout ll_business_licence;
    private LinearLayout ll_charge;
    private TextView tv_ent;
    private TextView tv_business_licence;
    private TextView tv_charge;
    private TextView tv_card;


    private RegisterBean registerBean;


    private List<RegisterPic> registerPics;

    private PhotoAdapter adapter;

    private TextView tv_pass;
    private TextView tv_refuse;
    private String add_user;
    private String region_code;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_detail);
        registerBean = (RegisterBean) getIntent().getSerializableExtra("bean");
        add_user = getIntent().getStringExtra("add_user");
        region_code = getIntent().getStringExtra("region_code");
        registerPics = new ArrayList<>();
        btnBack = findViewById(R.id.btnBack);
        btnBack.setOnClickListener(this);
        recyclerView = findViewById(R.id.recyclerView);
        ll_business_licence = findViewById(R.id.ll_business_licence);
        ll_charge = findViewById(R.id.ll_charge);
        tv_ent = findViewById(R.id.tv_ent);
        tv_business_licence = findViewById(R.id.tv_business_licence);
        tv_charge = findViewById(R.id.tv_charge);
        tv_card = findViewById(R.id.tv_card);
        tv_pass = findViewById(R.id.tv_pass);
        tv_pass.setOnClickListener(this);
        tv_refuse = findViewById(R.id.tv_refuse);
        tv_refuse.setOnClickListener(this);
        if (registerBean.getKind().equals("7")) {
            ll_business_licence.setVisibility(View.GONE);
            ll_charge.setVisibility(View.VISIBLE);
            if (registerBean.getCharge() == null) {
                tv_charge.setText(registerBean.getName());
            } else {
                tv_charge.setText(registerBean.getCharge());
            }
            registerPics.add(new RegisterPic("负责人(法定代表人)\n手持身份证正面", registerBean.getCharge_pic()));
            registerPics.add(new RegisterPic("负责人(法定代表人)\n身份证正面", registerBean.getId_card_face()));
            registerPics.add(new RegisterPic("负责人(法定代表人)\n身份证反面", registerBean.getId_card_back()));
        } else {
            ll_business_licence.setVisibility(View.VISIBLE);
            ll_charge.setVisibility(View.VISIBLE);
            tv_business_licence.setText(registerBean.getBusiness_number());
            tv_charge.setText(registerBean.getCharge());
            registerPics.add(new RegisterPic("负责人(法定代表人)\n手持身份证正面", registerBean.getCharge_pic()));
            registerPics.add(new RegisterPic("负责人(法定代表人)\n身份证正面", registerBean.getId_card_face()));
            registerPics.add(new RegisterPic("负责人(法定代表人)\n身份证反面", registerBean.getId_card_back()));
            registerPics.add(new RegisterPic("营业执照\n", registerBean.getBusiness_licence()));
        }
        tv_ent.setText(registerBean.getName());
        tv_card.setText(registerBean.getId_card());


        adapter = new PhotoAdapter();
        recyclerView.setLayoutManager(new GridLayoutManager(RegisterDetailActivity.this, 2));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(RegisterDetailActivity.this, MarkPicActivity.class);
                intent.putExtra("position", position);
                intent.putExtra("photos", (Serializable) registerPics);
                startActivity(intent);
            }
        });


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnBack:
                finish();
                break;
            case R.id.tv_pass:
                CustomMyDialog.Builder builder = new CustomMyDialog.Builder(RegisterDetailActivity.this);
                builder.setMessage("确认提交？");
                builder.setPositiveButton("确认提交", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        postCheckRegister("3", registerBean.getId(), "", add_user, region_code);
                    }
                });
                builder.setNegativeButton("下次再说", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.createNormalDialog().show();
                break;
            case R.id.tv_refuse:
                Intent intent = new Intent(RegisterDetailActivity.this, DialogActivity.class);
                intent.putExtra("add_user", add_user);
                intent.putExtra("region_code", region_code);
                intent.putExtra("ids", registerBean.getId());
                intent.putExtra("type", "2");
                intent.putExtra("model", "register");
                startActivityForResult(intent, 9021);
                break;
            default:
                break;
        }
    }


    public class PhotoAdapter extends BaseQuickAdapter<RegisterPic, BaseViewHolder> {
        public PhotoAdapter() {
            super(R.layout.product_pic_item, registerPics);
        }

        @Override
        protected void convert(BaseViewHolder helper, RegisterPic item) {

            Glide.with(mContext)
                    .load(item.getUrl()).placeholder(R.drawable.ic_launcher)
                    .into((ImageView) helper.getView(R.id.iv_iv));
            helper.setText(R.id.tv_name, item.getTitle());

        }
    }

    void postCheckRegister(String type, String ids, final String reason, String add_user, String region_code) {
        OkHttpUtils
                .post()
                .url(InitApplication.markUrl + "register/checkRegister.json")
                .addParams("addFrom", "1")
                .addParams("addMark", "android")
                .addParams("type", type)
                .addParams("ids", ids)
                .addParams("reason", reason)
                .addParams("add_user", add_user)
                .addParams("region_code", region_code)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(RegisterDetailActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            int code = jsonObject.optInt("code");
                            if (code == 0) {
                                Toast.makeText(RegisterDetailActivity.this, "提交成功", Toast.LENGTH_SHORT).show();
                                setResult(Activity.RESULT_OK);
                                finish();
                            } else {
                                Toast.makeText(RegisterDetailActivity.this, "提交失败，稍后再试", Toast.LENGTH_SHORT).show();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 9021:
                    setResult(Activity.RESULT_OK);
                    finish();
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

}
